package com.leyou.item.web;


import com.baomidou.mybatisplus.extension.api.R;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.*;

import com.leyou.item.entity.Sku;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuDetailService spuDetailService;

    /*
     *
     * todo 分页查询spu 条件齐全 查询商品列表的
     *
     * @param page
     * @param rows
     * @param brandId
     * @param categoryId
     * @param id
     * @param saleable
     * @return*/


    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> pageQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "saleable", required = false) Boolean saleable) {

        return ResponseEntity.ok(spuService.pageQuery(page, rows, brandId, categoryId, id, saleable));
    }


    /**
     * todo 根据商品id(spuId) 查询对应得规格参数组集合 以及组内参数信息 及对应值
     *
     * @param spuId
     * @param searching
     * @return
     */

    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> listSpecByspuId(
            @RequestParam("id") Long spuId,
            @RequestParam(value = "searching", required = false) Boolean searching) {

        return ResponseEntity.ok(spuService.listSpecByspuId(spuId, searching));


    }


    /**
     * todo 新增商品 涉及 3张表 spu spuDetail sku
     *
     * @param spuDTO
     * @return
     */

    @PostMapping("/spu")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO) {

        spuService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * todo 商品的上下架 更改saleable
     *
     * @param id
     * @param saleable
     * @return
     */

    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id") Long id, @RequestParam("saleable") Boolean saleable) {

        spuService.updateSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /*
     *
     * todo 根据商品的id 查看商品 查询商品 3表
     *
     * @param id
     * @return*/


    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id) {

        return ResponseEntity.ok(spuService.queryGoodsById(id));
    }
    /*
     *
     * todo 修改商品
     *
     * @param spuDTO
     * @return
     */

    @PutMapping("/spu")
    public ResponseEntity<Void> updateSpu(@RequestBody SpuDTO spuDTO) {

        spuService.updateSpu(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * todo 根据商品id 查询spuDetail
     *
     * @param id
     * @return
     */


    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id) {

        return ResponseEntity.ok(spuDetailService.querySpuDetailById(id));

    }

    //todo 根据SpuId查询SKU集合
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkusBySpuId(@RequestParam("id") Long id) {

        return ResponseEntity.ok(skuService.querySkusBySpuId(id));
    }


    /**
     * todo 根据skuid集合查询SKU集合
     *
     * @param ids
     * @return
     */

    @GetMapping("sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuIds(@RequestParam("ids") List<Long> ids) {

        return ResponseEntity.ok(skuService.querySkuBySpuIds(ids));

    }

    /**
     * todo 根据spu的Id查询spu，不包含其它信息
     *
     * @param id
     * @return
     */

    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuByid(@PathVariable("id") Long id) {

        return ResponseEntity.ok(spuService.querySpuByid(id));

    }

    /**
     * todo 清理库存  cartsMap就是本次下单所有购买的商品id 和数量
     *
     * @param cartsMap
     * @return
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusSku(@RequestBody Map<Long, Integer> cartsMap) {


        this.skuService.minusSku(cartsMap);

        return ResponseEntity.ok().build();
    }


}
